package com.eos.orm.c3p0.dao;

import java.sql.*;
import java.util.*;

import org.apache.commons.lang.StringUtils;

import com.eos.common.tool.CoreKeyContext;
import com.hf.utils.PaginationBean;
import com.hf.utils.StringUtil;

/**
 * 非mybatis操作持久化的DAO接口
 * 
 * @author Administrator
 */
public abstract class AbstractC3p0Dao<T> implements IDAO<T> {

	public boolean doCall() {
		return false;
	}

	public boolean save(List<T> t) {
		return false;
	}

	public boolean update(List<T> t) {
		return false;
	}

	public boolean remove(List<T> t) {
		return false;
	}

	/**
	 * select * from t_charge_order where pay_id='496wcknjldbhymrx' LIMIT 0,10
	 * 0表示起始是第1条数据 10表示每页的大小 分页查询数据
	 */
	public List<T> findPage(Map<String, String> map) {
		return null;
	}

	public List<T> findByWhere(Map<String, String> map) {
		return null;
	}

	/**
	 * 查询分页bean
	 * 
	 * @param map
	 *            {table_name,condition1,pagesize}
	 */
	public PaginationBean findBean(Map<String, String> map) {
		PaginationBean paginationbean = new PaginationBean();
		if (!StringUtil.isEmpty(map.get("page_size"))
				&& !StringUtil.isEmpty(map.get("table_name"))) {
			String sql = StringUtils.replace(StringUtils.replace(CoreKeyContext
					.getCoreKey("tabs_bean_sqlscript"), "$table_name$", map
					.get("table_name")), "$condition1$", condition1(map));
			Connection connection = DBConnectionManager.getConnection();
			PreparedStatement pstmt = null;
			ResultSet rs = null;
			try {
				pstmt = connection.prepareStatement(sql);
				rs = pstmt.executeQuery();
				if (rs.next()) {
					paginationbean.setMaxcount(rs.getInt(1));
				}
			} catch (SQLException e) {
				System.out.println(StringUtil.stringConcat(
						"com.eos.orm.c3p0.dao.AbstractC3p0Dao<T> table：", map
								.get("table_name"),
						" exec findPaginationBean throws exception:", e
								.getMessage()));
			} finally {
				freeResource(connection, pstmt, rs);
			}
			paginationbean.setPagesize(StringUtil
					.parseInt(map.get("page_size")));
			paginationbean.setPagecount(paginationbean.getMaxcount()
					% paginationbean.getPagesize() == 0 ? paginationbean
					.getMaxcount()
					/ paginationbean.getPagesize() : paginationbean
					.getMaxcount()
					/ paginationbean.getPagesize() + 1);
		}
		return paginationbean;
	}

	/**
	 * 组装分页查询语句中的condition2 limit 0,10 从第1条开始，每页10条
	 * 
	 * @param map
	 * @return
	 */
	public String condition2(Map<String, String> map) {
		StringBuilder condition = new StringBuilder();

		/**
		 * page_size或者 cur_page有为空的，则查询无数据
		 */
		if (StringUtil.isEmpty(map.get("page_size"))
				|| StringUtil.isEmpty(map.get("cur_page"))) {
			return condition.append("1=0").toString();
		}
		int page_size = StringUtil.parseInt(map.get("page_size"));
		int cur_page = StringUtil.parseInt(map.get("cur_page"));
		condition.append("limit ").append(page_size * (cur_page - 1)).append(
				",").append(page_size);

		return condition.toString();
	}

	/**
	 * 组装分页查询语句中的condition1 每个实体类各自重写本方法
	 * 
	 * @param map
	 * @return
	 */
	public String condition1(Map<String, String> map) {
		return " 1=1";
	}

	/**
	 * free database resource
	 * 
	 * @param connection
	 * @param stmt
	 * @param rs
	 */
	protected void freeResource(Connection connection, Statement stmt,
			ResultSet rs) {
		if (null != rs)
			try {
				rs.close();
			} catch (SQLException e) {
				System.out.println(StringUtil.stringConcat(
						"free ResultSet source is exception:", e.getMessage()));
			}
		if (null != stmt)
			try {
				stmt.close();
			} catch (SQLException e) {
				System.out.println(StringUtil.stringConcat(
						"free Statement source is exception:", e.getMessage()));
			}
		if (null != connection)
			try {
				connection.close();
			} catch (SQLException e) {
				System.out
						.println(StringUtil.stringConcat(
								"free Connection source is exception:", e
										.getMessage()));
			}
	}

	/**
	 * free database resource
	 * 
	 * @param connection
	 * @param pstmt
	 *            PreparedStatement
	 * @param rs
	 */
	protected void freeResource(Connection connection, PreparedStatement pstmt,
			ResultSet rs) {
		if (null != rs)
			try {
				rs.close();
			} catch (SQLException e) {
				System.out.println(StringUtil.stringConcat(
						"free ResultSet source is exception:", e.getMessage()));
			}
		if (null != pstmt)
			try {
				pstmt.close();
			} catch (SQLException e) {
				System.out.println(StringUtil.stringConcat(
						"free PreparedStatement source is exception:", e
								.getMessage()));
			}
		if (null != connection)
			try {
				connection.close();
			} catch (SQLException e) {
				System.out
						.println(StringUtil.stringConcat(
								"free Connection source is exception:", e
										.getMessage()));
			}
	}

	/**
	 * free database resource
	 * 
	 * @param connection
	 * @param cstmt
	 * @param rs
	 */
	protected void freeResource(Connection connection, CallableStatement cstmt,
			ResultSet rs) {
		if (null != rs)
			try {
				rs.close();
			} catch (SQLException e) {
				System.out.println(StringUtil.stringConcat(
						"free ResultSet source is exception:", e.getMessage()));
			}
		if (null != cstmt)
			try {
				cstmt.close();
			} catch (SQLException e) {
				System.out.println(StringUtil.stringConcat(
						"free CallableStatement source is exception:", e
								.getMessage()));
			}
		if (null != connection)
			try {
				connection.close();
			} catch (SQLException e) {
				System.out
						.println(StringUtil.stringConcat(
								"free Connection source is exception:", e
										.getMessage()));
			}
	}

}
